<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goodsList && goodsList.mainPictures">
      <!-- 面包屑 -->
      <XtxBread v-if="titleList.length !== 0">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${titleList[1].id}`">{{ titleList[1].name }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${titleList[0].id}`">{{ titleList[0].name }}</XtxBreadItem>
        <XtxBreadItem>{{ goodsList.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <goods-image :images="goodsList.mainPictures"></goods-image>
          <GoodsSales :good="goodsList"></GoodsSales>
        </div>
        <!-- 右盒子  商品规格详情区域 -->
        <div class="spec">
          <goods-name :good="goodsList" v-model="userAddress" />
          <!-- sku 销售属性 -->
          <sku :goods="goodsList" @change="skuChange"></sku>
          <xtx-numbox :max="10" :min="1" v-model="goodsNum"></xtx-numbox>
          <xtx-button
            size="large"
            type="primary"
            style="margin-top:20px;margin-left:60px"
            @click="subMitFn"
          >加入购物车</xtx-button>
        </div>
      </div>

      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <goods-tabs />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <goods-hot :type="1"></goods-hot>
          <goods-hot :type="2"></goods-hot>
          <goods-hot :type="3"></goods-hot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsTabs from './component/goods-tabs.vue'
import GoodsHot from './component/goods-hot.vue'
import { findGoods } from '@/api/goods'
import Sku from './Sku'
import { ref, onMounted, provide } from 'vue'
import { useRoute } from 'vue-router'
import GoodsImage from './component/goods-image.vue'
import GoodsName from './component/goods-name.vue'
import GoodsSales from './component/goods-sales.vue'
import { message } from '@/components/Message'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsImage,
    GoodsName,
    GoodsSales,
    Sku,
    GoodsTabs,
    GoodsHot
  },
  setup () {
    const goodsList = ref({})
    const titleList = ref([])
    const route = useRoute()

    // 用户地址
    const userAddress = ref({})

    // 商品数量
    const goodsNum = ref(1)

    // 商品信息
    const goodsInfo = ref({})

    // 获取到的信息传递给孙组件
    provide('goods-info', goodsList)

    async function getList () {
      const res = await findGoods(route.params.id)
      goodsList.value = res.result
      titleList.value = res.result.categories
    }

    onMounted(() => {
      getList()
    })

    // 商品信息回调
    function skuChange (data) {
      // console.log(data)
      goodsInfo.value = data
    }

    const store = useStore()

    async function subMitFn () {
      if (!goodsInfo.value.skuId) {
        return message({ type: 'error', text: '商品信息未全选' })
      } else {
        await store.dispatch('cart/asyncInsertCart', {
          skuId: goodsInfo.value.skuId,
          count: goodsNum.value
        })

        message({ type: 'success', text: '添加购物车成功' })
      }
    }
    return {
      goodsList,
      titleList,
      skuChange,
      subMitFn,
      userAddress,
      goodsNum,
      goodsInfo
    }
  }
}

</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
